package com.ruoyi.match.match.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.match.match.domain.bo.HistoryMatchOddsChangeBo;
import com.ruoyi.match.match.domain.vo.HistoryMatchOddsChangeVo;
import com.ruoyi.match.match.domain.HistoryMatchOddsChange;
import com.ruoyi.match.match.mapper.HistoryMatchOddsChangeMapper;
import com.ruoyi.match.match.service.IHistoryMatchOddsChangeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 比赛赔率变化Service业务层处理
 *
 * @author sjz
 * @date 2023-11-20
 */
@RequiredArgsConstructor
@Service
public class HistoryMatchOddsChangeServiceImpl implements IHistoryMatchOddsChangeService {

    private final HistoryMatchOddsChangeMapper baseMapper;

    /**
     * 查询比赛赔率变化
     */
    @Override
    public HistoryMatchOddsChangeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询比赛赔率变化列表
     */
    @Override
    public TableDataInfo<HistoryMatchOddsChangeVo> queryPageList(HistoryMatchOddsChangeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HistoryMatchOddsChange> lqw = buildQueryWrapper(bo);
        Page<HistoryMatchOddsChangeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询比赛赔率变化列表
     */
    @Override
    public List<HistoryMatchOddsChangeVo> queryList(HistoryMatchOddsChangeBo bo) {
        LambdaQueryWrapper<HistoryMatchOddsChange> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HistoryMatchOddsChange> buildQueryWrapper(HistoryMatchOddsChangeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HistoryMatchOddsChange> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMatchFlag()), HistoryMatchOddsChange::getMatchFlag, bo.getMatchFlag());
        lqw.eq(bo.getMainWinOdds() != null, HistoryMatchOddsChange::getMainWinOdds, bo.getMainWinOdds());
        lqw.eq(bo.getLevelOdds() != null, HistoryMatchOddsChange::getLevelOdds, bo.getLevelOdds());
        lqw.eq(bo.getGustWinOdds() != null, HistoryMatchOddsChange::getGustWinOdds, bo.getGustWinOdds());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeAttr()), HistoryMatchOddsChange::getTimeAttr, bo.getTimeAttr());
        lqw.eq(StringUtils.isNotBlank(bo.getTimeText()), HistoryMatchOddsChange::getTimeText, bo.getTimeText());
        return lqw;
    }

    /**
     * 新增比赛赔率变化
     */
    @Override
    public Boolean insertByBo(HistoryMatchOddsChangeBo bo) {
        HistoryMatchOddsChange add = BeanUtil.toBean(bo, HistoryMatchOddsChange.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改比赛赔率变化
     */
    @Override
    public Boolean updateByBo(HistoryMatchOddsChangeBo bo) {
        HistoryMatchOddsChange update = BeanUtil.toBean(bo, HistoryMatchOddsChange.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HistoryMatchOddsChange entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除比赛赔率变化
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
